package com.huynhchihieu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huynhchihieu.dao.BookDAO;
import com.huynhchihieu.domain.Book;

@Service
@Transactional
public class BookServiceImpl implements BookService {

	@Autowired
	BookDAO bookDAO;

	@Cacheable(value = "bookCached", key = "new Integer(#id).toString()")
	public Book findById(int id) {
		return bookDAO.findById(id);
	}

	@CacheEvict(value = "bookCached", allEntries = true)
	public void clearCached() {

	}

	public List<Book> getAllBooks() {
		return bookDAO.getAllBooks();
	}

	@CacheEvict(value="bookCached"/*, key="#book.bookName.replaceAll(\"\\s+\",\"\")"*/)
	public void updateBook(Book book) {
		bookDAO.upDate(book);
	}

	@CachePut(value="bookCached", key="#book.bookName.replaceAll(\"\\s+\",\"\")")
	public Book addBook(Book book) {
		Book booktmp = bookDAO.addBook(book);
		 return booktmp;
	}

	@Cacheable(value="bookCached", key="#bookName.replaceAll(\"\\s+\",\"\")")
	public Book finByName(String bookName) {
		return bookDAO.finByName(bookName);
	}

	@CacheEvict(value="bookCached"/*key="#book.bookName.replaceAll(\"\\s+\",\"\")"*/)
	public void delete(Book book) {
		bookDAO.delete(book);
	}

	
	public List<Book> finByNames(String bookName) {
		return bookDAO.finByNames(bookName);
	}



}
